package com.cunyu.util;

import cn.tom.kit.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class BaseUtil {
	
	public static String getTodayStr(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(new Date());
	}

	public static String getThisMonth(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		return df.format(new Date());
	}
	
	/**
	 * 获取下期时间   快乐8 9:05-23:55	| PK拾   9:07 - 23:57 	
	 * @param time
	 * @param minute
	 * @return
	 */
	public static Date addTimeMinute(Date time,int minute){
		Calendar ca = Calendar.getInstance();
		ca.setTime(time);
		ca.add(Calendar.MINUTE,minute);
		
		int h = ca.get(Calendar.HOUR_OF_DAY);
		int m = ca.get(Calendar.MINUTE);
		
		if((h>=23&&m>57)||h<9){
			ca.add(Calendar.HOUR_OF_DAY,9);
			ca.add(Calendar.MINUTE,5);
		}
		
		return ca.getTime();
	}
	

	/**
	 * yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @param formate
	 * @return
	 */
	public static Date parseToDate(String date, String formate){
		SimpleDateFormat df = new SimpleDateFormat(formate);
		Date d = null;
		try {
			d = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static String getFormatDate() {
		return new java.sql.Date(System.currentTimeMillis()).toString().replace("-", "");
	}
	
	/**
	 * 最新设备生成规则
	 * @param id
	 * @param pid 产品代码
	 * @param bid 厂商代码
	 * @return
	 */
	public static String  createId(String id,  String pid,  String bid){
		byte[] ids = ByteUtil.intToByteArray(Integer.parseInt(id)) ;
		
		/*异或校验*/
		byte idyh = ByteUtil.checkyh(ids);
		byte pyh=  (byte) (ByteUtil.checkyh(pid.getBytes()) ^ idyh ^ 0x5A);
		String pyh_ = Integer.toHexString(pyh&0xff);
		pyh_ = pyh_.length()==2?pyh_:("0"+pyh_);
		/*和校验*/
		long idhe = ByteUtil.SumCheck(ids);
		int bhe= (int) (ByteUtil.SumCheck(bid.getBytes()) + idhe+ 0xA5);
		byte b = (byte) bhe;
		String bhe_ = Integer.toHexString(b&0xff);
		bhe_ = bhe_.length()==2?bhe_:("0"+bhe_);
		return new StringBuffer().append(pid).append(pyh_).append(id).append(bhe_).append(bid).toString();
	}
	
	public static void main(String[] args) {
		System.out.println(createId("00002000", "G1A", "XY"));
	}
	
	/**
	 * 第一批的id生成规则
	 * @param id
	 * @param type
	 * @param type2
	 * @return
	 * @deprecated
	 */
	public static String  createId(String id,  char type,  String type2){
		int he = type; // 总和
		int swh = type; // 首尾和
		int endh = 0; // 最后两位和
		int i = 1;
		for (char c : id.toCharArray()) {
			int _c = Integer.parseInt("" + c);
			he += _c;
			if (i >= 7) {
				endh += _c;
			}
			if (i == 8) {
				swh += _c;
			}
			i++;
		}
		StringBuffer result = new StringBuffer(String.valueOf(type));
		result.append(id);
		String he16 = Integer.toHexString(he);
		String swh16 = Integer.toHexString(swh);
		String endh16 = Integer.toHexString(endh);
		result.append(he16.length() ==2 ? he16 : "0" + he16);
		result.append(swh16.length() == 2 ? swh16 : "0" + swh16);
		result.append(endh16.length() == 2 ? endh16 : "0" + endh16);
		result.append(type2);
		return result.toString();
	}
	
	/**
	 * @param str
	 * @return
	 */
	public static String format(int str){
		return String.format("%03d", str);
	}


	public static Date getDate(String target) {
		if(StringUtil.hasLength(target)){
			return new Date() ;
		}
		int len = target.length();
		if(len == 10){
			return parseToDate(target,"yyyy-MM-dd") ;
		}
		if(len == 19){
			return parseToDate(target,"yyyy-MM-dd HH:mm:ss") ;
		}
		return new Date() ;
	}
}
